package com.zzedu.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzedu.common.core.text.Convert;
import com.zzedu.system.domain.SysLogininfor;
import com.zzedu.system.mapper.SysLogininforMapper;
import com.zzedu.system.service.ISysLogininforService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 系统访问日志情况信息 服务层处理
 *
 * @author smarthome
 */
@Service
public class SysLogininforServiceImpl extends ServiceImpl<SysLogininforMapper, SysLogininfor> implements ISysLogininforService {

    /**
     * 新增系统登录日志
     *
     * @param logininfor 访问日志对象
     */
    @Override
    @Async
    public void insertLogininfor(SysLogininfor logininfor) {
        save(logininfor);
    }

    /**
     * 查询系统登录日志集合
     *
     * @param logininfor 访问日志对象
     * @return 登录记录集合
     */
    @Override
    public IPage<SysLogininfor> selectLogininforList(IPage<SysLogininfor> page, SysLogininfor logininfor) {
        LambdaQueryWrapper<SysLogininfor> wrapper = Wrappers.lambdaQuery(SysLogininfor.class);
        wrapper.like(StringUtils.isNotBlank(logininfor.getIpaddr()), SysLogininfor::getIpaddr, logininfor.getIpaddr());
        wrapper.eq(null != logininfor.getStatus(), SysLogininfor::getStatus, logininfor.getStatus());
        wrapper.like(StringUtils.isNotBlank(logininfor.getUserName()), SysLogininfor::getUserName, logininfor.getUserName());
        wrapper.orderByDesc(SysLogininfor::getId);
        return page(page, wrapper);
    }

    /**
     * 批量删除系统登录日志
     *
     * @param ids 需要删除的数据
     */
    @Override
    public int deleteLogininforByIds(String ids) {
        removeBatchByIds(Arrays.stream(Convert.toStrArray(ids)).collect(Collectors.toList()));
        return 1;
    }

    /**
     * 清空系统登录日志
     */
    @Override
    public void cleanLogininfor() {
        baseMapper.cleanLogininfor();
    }
}
